using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace GLDotNet
{
	public sealed partial class GLContext : DisposableObject
	{        
		INativeContext nativeContext;
		IGLContextListener listener;

		public int VersionMajor
		{
            get { return this.nativeContext.VersionMajor; }			
		}

		public int VersionMinor
		{
            get { return this.nativeContext.VersionMinor; }
		}

		public GLContext(INativeContext nativeContext)
			: this(nativeContext, new GLContextListener())
		{
		}

		public GLContext(INativeContext nativeContext, IGLContextListener listener)
		{
            if (nativeContext == null)
                throw new ArgumentNullException("nativeContext");

			this.nativeContext = nativeContext;
			this.listener = listener;
		}
        
        protected override void DisposeManagedResources()
        {
            this.nativeContext.Dispose();
            this.nativeContext = null;
        }

        protected override void DisposeUnmanagedResources()
        {
        }

        public bool MakeCurrent()
        {
            return this.nativeContext.MakeCurrent();
        }

		public bool SwapBuffers()
		{
            return this.nativeContext.SwapBuffers();
		}
		
		private object LoadExtensionFunction<T>()
		{
			return this.nativeContext.LoadExtensionFunction<T>("gl");
		}

		private void BeforeNativeFunctionCall(GLNativeFunctions function)
		{
			if (this.listener != null)
				this.listener.BeforeNativeFunctionCall(this, function);
		}

		private void AfterNativeFunctionCall(GLNativeFunctions function)
		{
			if (this.listener != null)
				this.listener.AfterNativeFunctionCall(this, function);
		}
	}
}
